package com.joysuch.wwyt.openapi.controllers.controllerV1;

import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.exception.ApiCoreException;
import com.joysuch.wwyt.dzhapi.dto.BuildDataDto;
import com.joysuch.wwyt.dzhapi.service.ApiTokenVerifyService;
import com.joysuch.wwyt.openapi.controllers.controllerV1.req.ApiV1BpJobQueryReq;
import com.joysuch.wwyt.openapi.controllers.controllerV1.req.ApiV1BpJobReq;
import com.joysuch.wwyt.openapi.controllers.controllerV1.req.ApiV1BpJobUpdateReq;
import com.joysuch.wwyt.openapi.controllers.controllerV1.req.ApiV1DataReq;
import com.joysuch.wwyt.openapi.controllers.controllerV1.vo.ApiPagePropertyDataVo;
import com.joysuch.wwyt.openapi.controllers.controllerV1.vo.ApiResultData;
import com.joysuch.wwyt.openapi.controllers.controllerV1.vo.ApiV1BpJobVo;
import com.joysuch.wwyt.openapi.exception.ApiV1ExceptionCodeMsg;
import com.joysuch.wwyt.openapi.result.ApiV1Result;
import com.joysuch.wwyt.openapi.services.serviceV1.ApiV1BpJobService;
import com.joysuch.wwyt.openapi.services.serviceV1.ApiV1TokenVerifyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author: JamesZY
 * @date: 2021/8/24
 * @description:岗位接口
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/job")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ApiV1BpJobController {
    private final ApiV1BpJobService service;
    private final ApiTokenVerifyService verifyService;

    /**
     * 批量新增
     */
    @PostMapping("/batchAdd")
    public ApiV1Result batchImport(@RequestBody @Valid ApiV1DataReq<ApiV1BpJobReq> req, HttpServletRequest request) {
        BuildDataDto buildDataDto;
        try {
            buildDataDto = verifyService.verifyToken(request);
        } catch (ApiV1ExceptionCodeMsg e) {
            return ApiV1Result.error(e.getCode(), e.getMessage());
        }
        try {
            List<ApiResultData> dataList = service.batchAdd(req.getData(),buildDataDto);
            return ApiV1Result.successData(dataList);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiV1Result.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public ApiV1Result update(@RequestBody @Valid ApiV1BpJobUpdateReq req, HttpServletRequest request) {
        BuildDataDto buildDataDto;
        try {
            buildDataDto = verifyService.verifyToken(request);
            service.update(req,buildDataDto);
        } catch (ApiV1ExceptionCodeMsg e) {
            return ApiV1Result.error(e.getCode(), e.getMessage());
        }
        return ApiV1Result.success();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchDelete")
    public ApiV1Result batchDelete(@RequestBody ApiV1DataReq<String> req, HttpServletRequest request) {
        BuildDataDto buildDataDto;
        try {
            buildDataDto = verifyService.verifyToken(request);
        } catch (ApiV1ExceptionCodeMsg e) {
            return ApiV1Result.error(e.getCode(), e.getMessage());
        }
        List<ApiResultData> dataList = service.deleteByIds(req.getData(),buildDataDto);
        return ApiV1Result.success(dataList);
    }

    /**
     * 分页查询
     */
    @PostMapping("/list")
    public ApiV1Result search(@RequestBody @Valid ApiV1BpJobQueryReq req, HttpServletRequest request) {
        ApiPagePropertyDataVo apiPagePropertyDataVo = null;
        BuildDataDto buildDataDto;
        try {
            buildDataDto = verifyService.verifyToken(request);
            apiPagePropertyDataVo = service.searchAll(req,buildDataDto);
        } catch (ApiV1ExceptionCodeMsg e) {
            return ApiV1Result.error(e.getCode(), e.getMessage());
        }
        return ApiV1Result.success(apiPagePropertyDataVo);
    }
}
